<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends Controller {
    public function login(){
        $this->display();
    }
     public function dologin(){
        $map['name']=I('post.name');
        $map['pass']=I('post.pass');
        require_once LIB_PATH. 'custom/securimage/securimage.php';
        $securimage = new\ Securimage();
        $captcha=I('post.captcha');
        if ($securimage->check($captcha) == false){
        $this->error('验证码错误','login',3);}else{
            $Newsmodel=M(User);
            $data=$Newsmodel->where($map)->find();
            if(empty($data)){
                $this->error("账号或密码错误") ;
            }else{               
                session('id',$data['id']);
                session('name',$data['name']);
                session('pass',$data['pass']);
                session('role',$data['role']);               
                session("isLogin",true);              
                $this->redirect('Admin/User/index');
            }
        }
     }
     public function logout() {
         session(null);
         $this->redirect('Admin/User/login');
     }
     public function index() { 
         if(session('isLogin')==FALSE){
         $this->redirect('Admin/User/login');}else{
             $this->display(); 
         }              
     }
    public function captcha() {
        require_once LIB_PATH. 'custom/securimage/securimage.php';
        $img = new \Securimage();
        if (!empty($_GET['namespace'])) $img->setNamespace($_GET['namespace']);
        $img->show();             
    }
    public function add() {
        $this->display();
    }
    public function doadd() {
        $Newsmodel=M(User);
        $data['name']=I('post.name');
        $data['pass']=I('post.pass');
        $data['role']=I('post.role');
       if(""!=trim($data['name']) &&""!=  trim($data['pass'])){
            $Newsmodel->add($data);
             $this->redirect('Admin/User/login');  
        }else{
          $this->error('请重新输入');     
        }
    }
    public function lists() {       
        $Newsmodel=M(User);
        $data=$Newsmodel->select();     
        $this->assign('data',$data);
        $this->display();     
    }
    public function delete() {
        $map['id']=I('get.id');
        if($map['id']== session('id')){
            $this->error('操作错误');}
        elseif(session('role')==1){
            $Newsmodel=M(User);
            $Newsmodel->where($map)->delete();
            $this->redirect('Admin/User/lists');
        }else{
                $this->error('你的权限不足');
        }   
    }
    public function update() {               
    if(session('role')==1){
        $Newsmodel=M(User);
        $map['id']=I('get.id');
        $data=$Newsmodel->where($map)->find();     
        $this->assign('data',$data);
        $this->display(); 
        }else{
             $this->error('你的权限不足');            
        }
    }
    public function doupdate() {
        $map['id']=I('post.id');
        $data['pass']=I('post.pass');
        $data['name']=I('post.name');
        $Newsmodel=M(User);
        $Newsmodel->where($map)->save($data);
        $this->success('更新成功','lists');
    }
}